//
//  GQMyEmptyCarController.m
//  Xanthium
//
//  Created by yangyongMac on 2018/10/28.
//  Copyright © 2018 GuanQinghao. All rights reserved.
//

#import "GQMyEmptyCarController.h"
#import "GQHTruckModel.h"
#import "GQHHeader.h"
#import "GQHCustomCell.h"

@class GQHTrucksCell;

@interface GQMyEmptyCarController () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView * tableView;
@property (nonatomic, strong) NSMutableArray * dataArray;

@end

@implementation GQMyEmptyCarController

- (NSMutableArray *)dataArray
{
    if (!_dataArray)
    {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.qh_titleLabel.text = @"我的发布";
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self getEmptyCarsList];
}

- (void)getEmptyCarsList
{
    [GQHTruckModel qh_fetchGetMyEmptyCarsWithParameters:@{@"userId":USER_GET_ID} success:^(BOOL status, NSNumber *code, NSString *message, NSArray<GQHTruckModel *> *trucks) {
        if (status)
        {
            if (trucks.count > 0)
            {
                [self.dataArray addObjectsFromArray:trucks];
                [self.tableView reloadData];
            }
            else
            {
                [self showToastWithText:@"暂无数据"];
            }
        } else {
            
            [self showToastWithText:message];
        }
    } failure:^(NSError *error) {
        
    }];
}

#pragma mark lazy loading...
-(UITableView *)tableView
{
    if (!_tableView)
    {
        CGFloat top = self.view.qh_statusBarHeight + self.view.qh_navigationBarHeight;
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, top , self.view.qh_width, self.view.qh_height - top - self.view.qh_safeAreaWidth - self.view.qh_homeIndicatorHeight) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.rowHeight = 80.0f;
        _tableView.tableFooterView = [UIView new];
        [self.view addSubview:_tableView];
    }
    return _tableView;
}

#pragma mark ------ UITableViewDataSource

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}

//先要设Cell可编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
}

//点击删除
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    //在这里实现删除操作
    GQHTruckModel *truckModel = self.dataArray[indexPath.row];
    [self deleteData:truckModel.qh_truckID];
    //删除数据，和删除动画
    [self.dataArray removeObjectAtIndex:indexPath.row];
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:indexPath.row inSection:0]] withRowAnimation:UITableViewRowAnimationTop];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cell";
    GQHTrucksCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        
        cell = [[GQHTrucksCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.qh_dialButton.tag = indexPath.row;
    
    GQHTruckModel *truckModel = self.dataArray[indexPath.row];
    /// 头像
    NSString *URLString = [[NSString qh_imageURLString] stringByAppendingPathComponent:truckModel.qh_driverAvatar];
    NSURL *URL = [NSURL URLWithString:URLString];
    [cell.qh_avatarImageView sd_setImageWithURL:URL placeholderImage:[UIImage qh_imageWithColor:kLightGrayColor]];
    /// 车辆信息
    cell.qh_nameLabel.text = [NSString stringWithFormat:@"%@/%@/%@吨",truckModel.qh_normalLength,truckModel.qh_model,truckModel.qh_normalLoading];;
    /// 起始地
    cell.qh_contentLabel.text = [NSString stringWithFormat:@"起始地: %@",truckModel.qh_departure];
    /// 目的地
    cell.qh_routeLabel.text = [NSString stringWithFormat:@"目的地: %@",truckModel.qh_destination];
    
    return cell;
}

#pragma mark ------- UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}

- (void)deleteData:(NSString *)ID
{
    [GQHTruckModel qh_fetchDeleteMyEmptyCarsWithParameters:@{@"id":ID} success:^(BOOL status, NSNumber *code, NSString *message, NSArray<GQHTruckModel *> *trucks) {
        if (status)
        {
            [self showToastWithText:@"删除成功"];
        }
        else
        {
            [self showToastWithText:message];
        }
    } failure:^(NSError *error) {
        
    }];
}

@end
